<?php
/**
 * 飞信机器人--功能插件--议题讨论
 * 功能：显示议题讨论
 * 数据表：
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
 支持参数：无
*/
/*
 数据表定义:
 分组表：
 
 议题表：
*/

class DiscussionPost extends CommonPlugin
{
	var $UUID='a3899ff0-08d7-11de-8c30-0800200c9a66';
	
	public function DiscussionPost(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	public function setPosition($pos)
	{
		$this->session->set("{$this->UUID}_position", $pos);
	}
	public function getPosition()
	{
		// In menu, pos=0, in group pos>0
		$pos = $this->session->getValue("{$this->UUID}_position");
		return $pos;		
	}	
	
	private function getPage($class_id)
	{
		$page = $this->session->getValue("{$this->UUID}_page");
		if( $page == 0) $page = 1;
		return $page;
	}
	
	private function setPage($class_id, $page)
	{
		$this->session->set("{$this->UUID}_page", $page);
	}
	
	private function getThreadID()
	{
		return $this->session->getValue("{$this->UUID}_threadid");
	}
	
	private function setThreadID($id)
	{
		$this->session->set("{$this->UUID}_threadid", $id);
	}
	
	private function getPostPage()
	{
		$page = $this->session->getValue("{$this->UUID}_postpage");
		return $id==0?1:$id;
	}
	
	private function setPostPage($page)
	{
		$this->session->set("{$this->UUID}_postpage", $id);
	}	
		
	public function getGroupList()
	{
		$sql = "select * from fx_discussion_group order by id";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$body = "<Font Face='楷体' Color='-16744320' Size='10.5'>{body}</Font>\n";
		
		$tmpBodyStr = "";
		for($i=0; $i<count($retArr); $i++)
		{
			$tmpBodyStr .= "[".($i+1)."] ".$retArr[$i]['group_name']."\n";
		}
		return str_replace("{body}", $tmpBodyStr, $body);
	}
	public function getTips()
	{
		$this->setPosition(0);
		$this->setThreadID(0);
		$msg = $this->session->currentMenu['tips']."\n";
		return "\n".$msg.$this->getGroupList().$this->getFooter('');		
	}
	
	public function getList($class_id)
	{
			// Get Count
			$where =  "where class_id=$class_id and verify=1";
			//if( $this->isAdmin() )$where =  "where class_id=$class_id";
			$order = "order by id desc";
			
			$sql = "select count(*) as count from fx_discussion {$where}";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			// page
			$perPage = 5;
			$pages = 1;
			$currentPage = $this->getPage($class_id);
			if($count>$perPage)
			{
				$pages = ceil($count/$perPage);
				$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
				$start = $perPage * ($currentPage-1);
				$sql = "select * from fx_discussion $where $order limit $start, $perPage";
			}else 
			{
				$extra = "";
				$sql = "select * from fx_discussion $where $order";
			}
			$retArr = $this->session->db->fetchQueryAll($sql);
			
			// generate list
			//return $sql;
			$msg  = "";
			if( count($retArr) >0 )
			{
				$msg.="\n==================== 分组议题列表 ====================\n\n";
				for($i=0; $i<count($retArr); $i++ )
				{
					$retArr[$i]['content'] = str_replace("\n","",$retArr[$i]['content']);
					$retArr[$i]['content'] = str_replace("\r","",$retArr[$i]['content']);
					$time = date('m-d H:i',$retArr[$i]['send_time']);
					$msg.="[".($i+1)."] ".$retArr[$i]['buddy_name']."  ".$time."  ".$retArr[$i]['content'];
					/*if($this->isAdmin())
					{
						$id = $retArr[$i]['id'];
						$msg.="(";
						$msg.=($retArr[$i]['verify']==0?"未审核,ID={$id}":"已审核,ID={$id}");
						$msg.=")";
					}*/
					$msg.="\n";
				}
			}else
			{
					$msg = "该分组下尚没有议题";	
			}
			
			return $msg.$this->getFooter($extra.",l-返回上级列表,数字-进入讨论区");
	}
	
	public function enterDiscussion($input)
	{
		$sql = "select * from fx_discussion_group order by id";
		$retArr = $this->session->db->fetchQueryAll($sql);
		
		$tmpBodyStr = "";
		for($i=0; $i<count($retArr); $i++)
		{
			if( intval($input) == ($i+1) )
			{
				$this->setPosition($retArr[$i]['id']);	
				return $this->getList($retArr[$i]['id']);
			}
		}
	}
	
	public function getPostList($id)
	{
			// Get Count
			$where =  "where tid=$id";
			$order = "order by id desc";
			
			$sql = "select count(*) as count from fx_discussion_posts {$where}";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			// page
			$perPage = 5;
			$pages = 1;
			$currentPage = $this->getPostPage();
			if($count>$perPage)
			{
				$pages = ceil($count/$perPage);
				$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
				$start = $perPage * ($currentPage-1);
				$sql = "select * from fx_discussion_posts $where $order limit $start, $perPage";
			}else 
			{
				$extra = "";
				$sql = "select * from fx_discussion_posts $where $order";
			}
			$retArr = $this->session->db->fetchQueryAll($sql);
			
			// generate list
			//return $sql;
			$msg  = "";
			if( count($retArr) >0 )
			{
				$msg.="\n==================== 议题讨论区 ====================\n\n";
				for($i=0; $i<count($retArr); $i++ )
				{
					$retArr[$i]['content'] = str_replace("\n","",$retArr[$i]['content']);
					$retArr[$i]['content'] = str_replace("\r","",$retArr[$i]['content']);
					$time = date('m-d H:i',$retArr[$i]['add_time']);
					$msg.="[".($i+1)."] ".$retArr[$i]['buddy_name']."  ".$time."  ".$retArr[$i]['content'];
					$msg.="\n";
				}
			}else
			{
					$msg = "该议题下尚无内容";	
			}
			return $msg.$this->getFooter($extra.",l-返回上级列表，或者直接发布讨论！");	
	}
	public function enterThread($input)
	{
		// Get Count
		$class_id = $this->getPosition();
		
		$where =  "where class_id=$class_id and verify=1";
		//if( $this->isAdmin() )$where =  "where class_id=$class_id";
		$order = "order by id desc";
		
		$sql = "select count(*) as count from fx_discussion {$where}";
		$retArr = $this->session->db->fetchQueryAll($sql);
		$count = $retArr[0]['count'];
		
		// page
		$perPage = 5;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
			$start = $perPage * ($currentPage-1);
			$sql = "select * from fx_discussion $where $order limit $start, $perPage";
		}else 
		{
			$extra = "";
			$sql = "select * from fx_discussion $where $order";
		}
		$retArr = $this->session->db->fetchQueryAll($sql);
		for($i=0; $i<count($retArr); $i++ )
		{
			if( intval($input) == ($i+1) )
			{
				$this->setThreadID($retArr[$i]['id']);	
				return $this->getPostList($retArr[$i]['id']);
			}
		}
	}
	
	public function writePost($input)
	{
		$tid = $this->getThreadID();
		$buddy_no = $this->session->buddyID;
		$buddy_name = mysql_real_escape_string($this->session->getBuddyName());
		$content = mysql_real_escape_string($input);
		$time = time(NULL);
		
		$sql = "insert into fx_discussion_posts(tid,buddy_no,buddy_name,content,add_time) ";
		$sql.= " values({$tid},{$buddy_no},'{$buddy_name}','{$content}',{$time})";
		$this->session->db->execute($sql);
		
		return "谢谢您的参与。您可以继续发布！\n". $this->getFooter(',l-返回上级列表');	
	}
	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
		if( $this->getThreadID() != 0)	// 发贴模式
		{
			if( $input == 'l')
			{	
				//return "ccc";
				$this->setThreadID(0);
				return $this->enterDiscussion($this->getPosition());	//回上一菜单
			}
			return $this->writePost($input);
		}
		
		if( $this->getPosition() == 0 )	// In main menu
		{
			return $this->enterDiscussion($input);
		}else
		{
			if($input == "l")
			{
				return $this->getTips();
			}
			
			// Get Count
			$class_id = $this->getPosition();
			$where =  "where class_id=$class_id and verify=1 order by id desc";
			//if( $this->isAdmin() )$where =  "where class_id=$class_id";
			$order = "order by id desc";
			
			$sql = "select count(*) as count from fx_discussion {$where}";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			$perPage = 5;
			$pages = 1;
			$currentPage = $this->getPage($class_id);
			if($count>$perPage)
			{
				$pages = ceil($count/$perPage);
			}
			
			if( $input == 'p' )	//上一页
			{
				if($currentPage>1)
				{
					$currentPage--;
					$this->setPage($class_id, $currentPage);
				}
				return $this->getList($class_id);
			}else if( $input == 'n') //下一页
			{
				echo $currentPage."***{$pages}********************************".$this->getPage($class_id)."\n";
				if($currentPage<$pages)
				{
					$currentPage++;
					$this->setPage($class_id, $currentPage);
					
					echo $currentPage."***********************************".$this->getPage($class_id)."\n";
				}
				return $this->getList($class_id);
			}else if(is_numeric($input)) // 进入相应的主题
			{
				return $this->enterThread($input);
			}
		}
		return "不支持的指令\n". $this->getFooter();
	}
}
?>